package com.wslNB.controller;

import com.wslNB.feign.UserFeignClient;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {


    @Autowired
    private UserFeignClient userFeignClient;
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/checkNumber")
    public String checkNumber() {

        String checkNumber = userFeignClient.checkNumber();
        System.out.println(checkNumber);
        return checkNumber;
    }

    //用户提交表单
    @PostMapping("/userLogin")
    public ModelAndView userLogin(@RequestBody Map<String, Object> map, ModelAndView modelAndView) {
        if (map.isEmpty()) {
            return null;
        }

        if (map.get("checkNumber") != null) {
            //获取redis中的验证码
            Integer checkNumber = (Integer) redisTemplate.boundValueOps("checkNumber").get();
            String checkCode = String.valueOf(checkNumber);
            String checkNumber1 = (String) map.get("checkNumber");

            //校验验证码成功
            if (checkCode.equals(checkNumber1)) {
                String phone = (String) map.get("phone");
                String password = (String) map.get("password");
                com.wslNB.domain.User user1 = new com.wslNB.domain.User();
                //远程调用服务
                User user = userFeignClient.findByPhoneAndPassword(user1);
                if (user != null) {
                    //登录成功，跳转页面
                    modelAndView.setViewName("list.html");
                    return modelAndView;
                }
            }
        }

        return null;
    }

}
